﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using VanTaiBLL;
using VanTaiData;
using VanTaiProperty;
using Roles;
using System.Web.UI.HtmlControls;
namespace VanTaiTMS.Page
{
    public partial class bao_cao_doanh_thu : System.Web.UI.Page
    {
        #region[Define]
        private DataTable dtSaleReport;
        private DataTable dtSaleReportEdit;
        private DataTable dtSaleReportFillter;
        private DataTable dtTotal;
        private DataTable dtTotal_All;
        private DataTable dtSearch;
        protected string SuggestionList;
        protected string StrAlert = "";
        private DataTable dtAutoComplete;
        protected string StrTOTAL_Revenue = "";
        protected string StrTOTAL_COSTS = "";
        protected string StrTOTAL_Profit_Loss = "";
        private clsTMSalesReportBusiness clsSalesReport = new clsTMSalesReportBusiness();
        #endregion

        #region[Controller]
        protected void Page_Load(object sender, EventArgs e)
        {
            CheckPermisstion();
            cmdDuyet.Visible = false;
            Autocomplate();
            Total_All();
            if (!IsPostBack)
            {
                BindGridclsSalesReport();
            }
        }
        protected void cmdAddNew_Click(object sender, EventArgs e)
        {
            //flag = true;
            //lbThongBaoLoi.Text = "";
            //ClearTextbox();
            //ClearDropdownlist();
            //txtMaDieuDong.Text = "QG000" + MaTuDong();
            //OpenDialog("#baocaodoanhthu");
            //imgSave.ToolTip = "Thêm mới";
            //upNewUpdatePanel.Update();
        }
        protected void cmdEdit_Click(object sender, EventArgs e)
        {
            //for (int i = 0; i < ListViewAll.Items.Count; i++)
            //{
            //    CheckBox chkView = (CheckBox)ListViewAll.Items[i].FindControl("chkList");
            //    Label lbTrangThai = (Label)ListViewAll.Items[i].FindControl("lbDuyet");
            //    if (chkView != null)
            //    {
            //        if (chkView.Checked)
            //        {
            //            if (lbTrangThai.Text == "1")
            //            {
            //                imgSave.Enabled = false;
            //                BindGridLenhdieuDongEdit(Convert.ToInt32(ListViewAll.DataKeys[i].Value.ToString()));
            //                OpenDialog("#baocaodoanhthu");
            //                upNewUpdatePanel.Update();
            //                imgSave.ToolTip = "Cập nhật";
            //                flag = false;
            //            }
            //            else
            //            {
            //                imgSave.Enabled = true;
            //                BindGridLenhdieuDongEdit(Convert.ToInt32(ListViewAll.DataKeys[i].Value.ToString()));
            //                OpenDialog("#baocaodoanhthu");
            //                upNewUpdatePanel.Update();
            //                imgSave.ToolTip = "Cập nhật";
            //                flag = false;
            //            }
            //        }
            //    }
            //}
        }
        protected void cmdDel_Click(object sender, EventArgs e)
        {
            for (int i = 0; i < ListViewAll.Items.Count; i++)
            {
                CheckBox chkHide = (CheckBox)ListViewAll.Items[i].FindControl("chkList");
                Label lbTrangThai = (Label)ListViewAll.Items[i].FindControl("lbDuyet");
                if (chkHide != null)
                {
                    if (chkHide.Checked)
                    {
                        try
                        {
                            VanTaiBLL.clsTMSalesReportBusiness.DeleteTMSalesReport(int.Parse(ListViewAll.DataKeys[i].Value.ToString()));
                            Total_All();
                            BindGridclsSalesReport();
                        }
                        catch (Exception ex)
                        {
                            lbThongBaoXoa.Text = ex.ToString();
                            OpenDialog("#thongbaoxoa");
                            upDatePanelThongBaoXoa.Update();
                            cmdAgree.Visible = false;
                            cmdClose.Text = "Đóng lại";
                        }
                    }
                }
            }
        }
        protected void cmdSerach_Click(object sender, EventArgs e)
        {
            if (txtSearch.Text == "")
            {
                StrAlert += "<div class='alert alert-warning'>";
                StrAlert += "<a href='#' class='close' data-dismiss='alert'>&times;</a>";
                StrAlert += "Vui lòng nhập vào từ khoá để tìm kiếm !";
                StrAlert += "</div>";
            }
            else
            {
                Search(txtSearch.Text);
            }
        }
        protected void cmdRefesh_Click(object sender, EventArgs e)
        {
            txtSearch.Text = "";
            txtTuNgayNgayNhapKho.Text = "";
            txtDenNgayNhapKho.Text = "";
            BindGridclsSalesReport();
            Total_All();
        }
        protected void cmdFilter_Click(object sender, EventArgs e)
        {
            Fillter(txtTuNgayNgayNhapKho.Text, txtDenNgayNhapKho.Text);
            Total(txtTuNgayNgayNhapKho.Text, txtDenNgayNhapKho.Text);
        }
        protected void cmdDuyet_Click(object sender, EventArgs e)
        {
            //-------------------------------------------
            //try
            //{
            //    for (int i = 0; i < ListViewAll.Items.Count; i++)
            //    {
            //        CheckBox chkView = (CheckBox)ListViewAll.Items[i].FindControl("chkList");
            //        if (chkView != null)
            //        {
            //            if (chkView.Checked)
            //            {
            //                BindGridEmail(Convert.ToInt32(ListViewAll.DataKeys[i].Value.ToString()));
            //                OpenDialog("#duyetlenhdieudong");
            //                upDatePanelDuyetLenh.Update();
            //            }
            //        }
            //    }
            //}
            //catch (Exception ex)
            //{
            //    lbThongBaoLoi.Text = ex.ToString();
            //    OpenDialog("#thongbao");
            //    updatePanelThongBao.Update();
            //}
        }
        protected void cmdExport_Click(object sender, EventArgs e)
        {
            OpenDialog("#exporttoexcel");
        }
        protected void cmdFillterExport_Click(object sender, ImageClickEventArgs e)
        {
            //FillterExport(txtTuNgayNhapKhoExport.Text, txtDenNgayNhapKhoExport.Text);
        }
        protected void cmdExportToExcel_Click(object sender, EventArgs e)
        {
            //if (txtNameReport.Text == "")
            //{
            //    lbThongBaoLoi.Text = "Vui lòng nhập vào tên tập tin để xuất ra tập tin bảng tính !";
            //    OpenDialog("#thongbao");
            //    updatePanelThongBao.Update();
            //}
            //else
            //{
            //    Response.Clear();
            //    Response.AddHeader("content-disposition", "attachment;filename=" + txtNameReport.Text + ".xls");
            //    Response.Charset = "";
            //    Response.ContentType = "application/vnd.xls";
            //    System.IO.StringWriter stringWrite = new System.IO.StringWriter();
            //    System.Web.UI.HtmlTextWriter htmlWrite = new HtmlTextWriter(stringWrite);
            //    PaneViewData.RenderControl(htmlWrite);
            //    HttpContext.Current.Response.Write(stringWrite.ToString());
            //    HttpContext.Current.Response.End();
            //    upDatePanelExportToExcel.Update();
            //}
        }
        //---------------button-popup---------------------
        protected void imgrefesh_Click(object sender, ImageClickEventArgs e)
        {

        }
        protected void imgSave_Click(object sender, ImageClickEventArgs e)
        {
            if (txtSoChang.Text == "")
            {
                lbThongBaoLoi.Text = "Vui lòng điền vào số chặng !";
                OpenDialog("#thongbao");
                updatePanelThongBao.Update();
            }
            if (txtDoanhThu.Text == "")
            {
                lbThongBaoLoi.Text = "Vui lòng điền vào doanh thu !";
                OpenDialog("#thongbao");
                updatePanelThongBao.Update();
            }
            if (txtLitDau.Text == "")
            {
                lbThongBaoLoi.Text = "Vui lòng điền vào số lít dầu !";
                OpenDialog("#thongbao");
                updatePanelThongBao.Update();
            }
            if (txtTienDau.Text == "")
            {
                lbThongBaoLoi.Text = "Vui lòng điền vào số tiền dầu !";
                OpenDialog("#thongbao");
                updatePanelThongBao.Update();
            }
            if (txtLuong.Text == "")
            {
                lbThongBaoLoi.Text = "Vui lòng điền vào số lương !";
                OpenDialog("#thongbao");
                updatePanelThongBao.Update();
            }
            if (txtLoPhi.Text == "")
            {
                lbThongBaoLoi.Text = "Vui lòng điền vào lộ phí !";
                OpenDialog("#thongbao");
                updatePanelThongBao.Update();
            }
            if (txtLuongPhuXe.Text == "")
            {
                lbThongBaoLoi.Text = "Vui lòng điền vào lương phụ xe !";
                OpenDialog("#thongbao");
                updatePanelThongBao.Update();
            }
            if (txtVeCauDuong.Text == "")
            {
                lbThongBaoLoi.Text = "Vui lòng điền vào vẽ cầu đường đường !";
                OpenDialog("#thongbao");
                updatePanelThongBao.Update();
            }
            if (txtVeCauDuong.Text == "")
            {
                lbThongBaoLoi.Text = "Vui lòng điền vào bốc xếp !";
                OpenDialog("#thongbao");
                updatePanelThongBao.Update();
            }
            if (txtGiaoTeCA.Text == "")
            {
                lbThongBaoLoi.Text = "Vui lòng điền vào giao tế CA !";
                OpenDialog("#thongbao");
                updatePanelThongBao.Update();
            }
           
            if (txtDinhPhi.Text == "")
            {
                lbThongBaoLoi.Text = "Vui lòng điền vào định phí !";
                OpenDialog("#thongbao");
                updatePanelThongBao.Update();
            }
            if (txtSoChang.Text != "" && txtDoanhThu.Text != "" &&
                txtLitDau.Text != "" && txtTienDau.Text != "" &&
                txtLuong.Text != "" && txtLoPhi.Text != "" &&
                txtLuongPhuXe.Text != "" && txtVeCauDuong.Text != "" &&
                txtBocXep.Text != "" && txtGiaoTeCA.Text != "" &&
                txtDinhPhi.Text != "")
            {
                UpdateSalesReport();
            }
        }
        protected void cmdAgree_Click(object sender, EventArgs e)
        {
            
        }
        protected void cmdClose_Click(object sender, EventArgs e)
        {
            CloseDialog("#thongbaoxoa");
        }
        protected void cmdCloseThongBao_Click(object sender, EventArgs e)
        {
            CloseDialog("#thongbao");
        }
        protected void ImgXetDuyet_Click(object sender, ImageClickEventArgs e)
        {
            //BrowserNV();
        }
        protected void ImgRefeshDuyet_Click(object sender, ImageClickEventArgs e)
        {

        }
        protected void ImgRefeshExport_Click(object sender, ImageClickEventArgs e)
        {
            //txtTuNgayNhapKhoExport.Text = "";
            //txtDenNgayNhapKhoExport.Text = "";
            //BindGridLenhDieuDongExportExcel();
        }
        //-------------list-view-----------------------
        protected void ListViewAll_ItemCommand(object sender, ListViewCommandEventArgs e)
        {
            try
            {
                HttpCookie cookie = Request.Cookies["UserLogin"];
                bool Edit = SecurityServices.HasPermisstion(StringContent.QuanLyDoanhThuChiPhi, int.Parse(cookie["RolesID"]), TypeaAudit.Edit);
                lbThongBaoLoi.Text = "";
                txtMaDieuDong.Enabled = false;
                txtNgayDieuDong.Text = String.Format("{0:dd/MM/yyyy}", DateTime.Now.ToString());
                txtSoXe.Enabled = false;
                txtTaiXe.Enabled = false;
                txtNhomXe.Enabled = false;
                txtNoiBatDau.Enabled = false;
                txtNoiKetThuc.Enabled = false;
                txtLoaiCont.Enabled = false;
                txtKhachHang.Enabled = false;

                txtTongBienPhi.Enabled = false;
                txtTongChiPhi.Enabled = false;
                txtLaiLo.Enabled = false;
                if (e.CommandName == "Accept" || e.CommandName == "Accept_A" || e.CommandName == "Accept_B" ||
                    e.CommandName == "Accept_C" || e.CommandName == "Accept_D")
                {
                    if (Edit == false)
                    {
                        imgSave.Visible = false;
                        string SenderId = ((HtmlInputHidden)e.Item.FindControl("hiddenId")).Value;
                        BindGridSaleReportDetail_Edit(int.Parse(SenderId));
                        OpenDialog("#baocaodoanhthu");
                        upNewUpdatePanel.Update();
                        imgSave.ToolTip = "Cập nhật";
                    }
                    else
                    {
                        imgSave.Visible = true;
                        string SenderId = ((HtmlInputHidden)e.Item.FindControl("hiddenId")).Value;
                        BindGridSaleReportDetail_Edit(int.Parse(SenderId));
                        OpenDialog("#baocaodoanhthu");
                        upNewUpdatePanel.Update();
                        imgSave.ToolTip = "Cập nhật";
                    }
                }
            }
            catch (Exception ex)
            {
                lbThongBaoLoi.Text = ex.ToString();
                OpenDialog("#thongbao");
                updatePanelThongBao.Update();
            }
        }
        protected void ListViewAll_ItemDataBound(object sender, ListViewItemEventArgs e)
        {
            if (e.Item.ItemType == ListViewItemType.DataItem)
            {
                LinkButton linkListName = (LinkButton)e.Item.FindControl("linkListName");
                LinkButton linkListName_A = (LinkButton)e.Item.FindControl("linkListName_A");
                LinkButton linkListName_B = (LinkButton)e.Item.FindControl("linkListName_B");
                LinkButton linkListName_C = (LinkButton)e.Item.FindControl("linkListName_C");
                LinkButton linkListName_D = (LinkButton)e.Item.FindControl("linkListName_D");

                string _jsDouble1 = ClientScript.GetPostBackClientHyperlink(linkListName, "");
                string _jsDouble2 = ClientScript.GetPostBackClientHyperlink(linkListName_A, "");
                string _jsDouble3 = ClientScript.GetPostBackClientHyperlink(linkListName_B, "");
                string _jsDouble4 = ClientScript.GetPostBackClientHyperlink(linkListName_C, "");
                string _jsDouble5 = ClientScript.GetPostBackClientHyperlink(linkListName_D, "");

                linkListName.Attributes["ondblclick"] = _jsDouble1;
                linkListName.Attributes["onclick"] = "return false;";

                linkListName_A.Attributes["ondblclick"] = _jsDouble2;
                linkListName_A.Attributes["onclick"] = "return false;";

                linkListName_B.Attributes["ondblclick"] = _jsDouble3;
                linkListName_B.Attributes["onclick"] = "return false;";

                linkListName_C.Attributes["ondblclick"] = _jsDouble4;
                linkListName_C.Attributes["onclick"] = "return false;";

                linkListName_D.Attributes["ondblclick"] = _jsDouble5;
                linkListName_D.Attributes["onclick"] = "return false;";
            }
        }
        //-----------pagger-----------------------
        protected void DataPagerListAll_PreRender(object sender, EventArgs e)
        {
            DataPagerListAll.PageSize = 17;
            BindGridclsSalesReport();
        }
        //---------Check-all--------------
        protected void chkCheckAll_CheckedChanged(object sender, EventArgs e)
        {

        }
        //---------DropDownlist---------------------
        protected void drTrangThai_SelectedIndexChanged(object sender, EventArgs e)
        {

        }
        protected void drSoXe_SelectedIndexChanged(object sender, EventArgs e)
        {
            //BindGridNhomXe(int.Parse(drSoXe.SelectedValue.ToString()));
            //BindGridTaiXe(int.Parse(drSoXe.SelectedValue.ToString()));
        }
        protected void drTaiXe_SelectedIndexChanged(object sender, EventArgs e)
        {
            //drNhomXe.Enabled = true;
        }
        #endregion

        #region[Function]
        protected void BindGridclsSalesReport()
        {
            dtSaleReportFillter = clsSalesReport.TMSalesReport_Fillter_Date(txtTuNgayNgayNhapKho.Text, txtDenNgayNhapKho.Text);
            dtSaleReport = clsSalesReport.Search(txtSearch.Text);
            if (txtTuNgayNgayNhapKho.Text == "" && txtDenNgayNhapKho.Text == "")
            {
                if (dtSaleReport != null && dtSaleReport.Rows.Count > 0)
                {
                    ListViewAll.DataSource = dtSaleReport;
                    ListViewAll.DataBind();
                    ListViewAll.Visible = true;
                    DataPagerListAll.Visible = true;

                    ListViewExport.DataSource = dtSaleReport;
                    ListViewExport.DataBind();
                    ListViewExport.Visible = true;
                }
                else
                {
                    ListViewAll.Visible = false;
                    DataPagerListAll.Visible = false;

                    ListViewExport.Visible = false;
                }
            }
            else
            {
                if (dtSaleReportFillter != null && dtSaleReportFillter.Rows.Count > 0)
                {
                    ListViewAll.DataSource = dtSaleReportFillter;
                    ListViewAll.DataBind();
                    ListViewAll.Visible = true;
                    DataPagerListAll.Visible = true;

                    ListViewExport.DataSource = dtSaleReportFillter;
                    ListViewExport.DataBind();
                    ListViewExport.Visible = true;
                }
            }
        }
        protected void BindGridSaleReportDetail_Edit(int id)
        {
            dtSaleReportEdit = clsSalesReport.BindGirdTMSalesReport_Detail_Edit(id);
            if (dtSaleReportEdit != null && dtSaleReportEdit.Rows.Count > 0)
            {
                lbID.Text = dtSaleReportEdit.Rows[0]["SalesReport_ID"].ToString();
                lbIDXoa.Text = dtSaleReportEdit.Rows[0]["SalesReport_ID"].ToString();
                txtMaDieuDong.Text = dtSaleReportEdit.Rows[0]["SalesReport_TransferOrder_ID"].ToString();
                txtNgayDieuDong.Text = String.Format("{0:dd/MM/yyyy}", dtSaleReportEdit.Rows[0]["SalesReport_Date"].ToString());
                txtSoXe.Text = dtSaleReportEdit.Rows[0]["Vehicle_Number"].ToString();
                txtTaiXe.Text = dtSaleReportEdit.Rows[0]["FullNameMem"].ToString();
                txtNhomXe.Text = dtSaleReportEdit.Rows[0]["VehicleGroup_Name_Old"].ToString();
                txtKhachHang.Text = dtSaleReportEdit.Rows[0]["Customers_Name"].ToString();
                txtNoiBatDau.Text = dtSaleReportEdit.Rows[0]["Delivery_Name_REQUEST_STOCK"].ToString();
                txtNoiKetThuc.Text = dtSaleReportEdit.Rows[0]["Delivery_Name_RESPONE_STOCK"].ToString();
                txtLoaiCont.Text = dtSaleReportEdit.Rows[0]["Cont_Name"].ToString();
                txtSoChang.Text = dtSaleReportEdit.Rows[0]["SalesReport_Direction"].ToString();
                txtDoanhThu.Text = String.Format("{0:0,00}", dtSaleReportEdit.Rows[0]["SalesReport_Revenue"]).Replace(".", ",");
                txtLitDau.Text = String.Format("{0:0,00}", dtSaleReportEdit.Rows[0]["SalesReport_Liters_Of_Oil"]).Replace(".", ",");
                txtTienDau.Text = String.Format("{0:0,00}", dtSaleReportEdit.Rows[0]["SalesReport_Oil_Money"]).Replace(".", ",");
                txtLuong.Text = String.Format("{0:0,00}", dtSaleReportEdit.Rows[0]["SalesReport_Salary"]).Replace(".", ",");
                txtLoPhi.Text = String.Format("{0:0,00}", dtSaleReportEdit.Rows[0]["SalesReport_Cost"]).Replace(".", ",");
                txtLuongPhuXe.Text = String.Format("{0:0,00}", dtSaleReportEdit.Rows[0]["SalesReport_Car_Salary"]).Replace(".", ",");
                txtVeCauDuong.Text = String.Format("{0:0,00}", dtSaleReportEdit.Rows[0]["SalesReport_Bridges"]).Replace(".", ",");
                txtBocXep.Text = String.Format("{0:0,00}", dtSaleReportEdit.Rows[0]["SalesReport_Stevedorage"]).Replace(".", ",");
                txtGiaoTeCA.Text = String.Format("{0:0,00}", dtSaleReportEdit.Rows[0]["SalesReport_Wage"]).Replace(".", ",");
                txtDinhPhi.Text = String.Format("{0:0,00}", dtSaleReportEdit.Rows[0]["SalesReport_Fixed_Cost"]).Replace(".", ",");
                txtTongBienPhi.Text = String.Format("{0:0,00}", dtSaleReportEdit.Rows[0]["SalesReport_Total_Minutes"]).Replace(".", ",");
                txtTongChiPhi.Text = String.Format("{0:0,00}", dtSaleReportEdit.Rows[0]["SalesReport_Total_Costs"]).Replace(".", ",");
                txtLaiLo.Text = String.Format("{0:0,00}", dtSaleReportEdit.Rows[0]["SalesReport_Profit_Loss"]).Replace(".", ",");
                txtGhiChu.Text = String.Format("{0:0,00}", dtSaleReportEdit.Rows[0]["SalesReport_Note"]).Replace(".", ",");
            }
        }
        protected void UpdateSalesReport()
        {
            try
            {
                VanTaiProperty.clsTMSalesReportProperty obj = new clsTMSalesReportProperty();
                obj.SalesReport_ID = int.Parse(lbID.Text);
                obj.SalesReport_Date = DateTime.Parse(txtNgayDieuDong.Text);
                obj.SalesReport_Direction = int.Parse(txtSoChang.Text.Replace(",", ""));
                obj.SalesReport_Revenue = float.Parse(txtDoanhThu.Text.Replace(",", ""));
                obj.SalesReport_Liters_Of_Oil = float.Parse(txtLitDau.Text.Replace(",", ""));
                obj.SalesReport_Oil_Money = float.Parse(txtTienDau.Text.Replace(",", ""));
                obj.SalesReport_Salary = float.Parse(txtLuong.Text.Replace(",", ""));
                obj.SalesReport_Cost = float.Parse(txtLoPhi.Text.Replace(",", ""));
                obj.SalesReport_Car_Salary = float.Parse(txtLuongPhuXe.Text.Replace(",", ""));
                obj.SalesReport_Bridges = float.Parse(txtVeCauDuong.Text.Replace(",", ""));
                obj.SalesReport_Stevedorage = float.Parse(txtBocXep.Text.Replace(",", ""));
                obj.SalesReport_Wage = float.Parse(txtGiaoTeCA.Text.Replace(",", ""));

                txtTongBienPhi.Text = Convert.ToString(Math.Round(float.Parse(txtSoChang.Text.Replace(",", ""))
                               + float.Parse(txtDoanhThu.Text.Replace(",", "")) + float.Parse(txtLitDau.Text.Replace(",", ""))
                               + float.Parse(txtTienDau.Text.Replace(",", "")) + float.Parse(txtLuong.Text.Replace(",", ""))
                               + float.Parse(txtLoPhi.Text.Replace(",", "")) + float.Parse(txtLuongPhuXe.Text.Replace(",", ""))
                               + float.Parse(txtVeCauDuong.Text.Replace(",", "")) + float.Parse(txtBocXep.Text.Replace(",", ""))
                               + float.Parse(txtGiaoTeCA.Text.Replace(",", "")) + float.Parse(txtDinhPhi.Text.Replace(",", ""))));

                obj.SalesReport_Total_Minutes = float.Parse(txtTongBienPhi.Text.Replace(",", ""));
                txtTongBienPhi.Text = String.Format("{0:0,00}", float.Parse(txtTongBienPhi.Text).ToString("0,00").Replace(".", ","));

                obj.SalesReport_Fixed_Cost = float.Parse(txtDinhPhi.Text.Replace(",", ""));

                txtTongChiPhi.Text = Convert.ToString(Math.Round(float.Parse(txtTongBienPhi.Text.Replace(",", "")) + float.Parse(txtDinhPhi.Text.Replace(",", ""))));
                obj.SalesReport_Total_Costs = float.Parse(txtTongChiPhi.Text.Replace(",", ""));
                txtTongChiPhi.Text = String.Format("{0:0,00}", (float.Parse(txtTongChiPhi.Text.Replace(".", ","))));

                txtLaiLo.Text = Convert.ToString(Math.Round(float.Parse(txtDoanhThu.Text.Replace(",", "")) - float.Parse(txtTongChiPhi.Text.Replace(",", ""))));
                obj.SalesReport_Profit_Loss = float.Parse(txtLaiLo.Text.Replace(",", ""));
                txtLaiLo.Text = String.Format("{0:0,00}", float.Parse(txtLaiLo.Text).ToString("0,00").Replace(".", ","));
                obj.SalesReport_Note = txtGhiChu.Text;
                VanTaiBLL.clsTMSalesReportBusiness.UpdateTMSalesReport(obj);
                lbThongBaoLoi.Text = "Cập nhật thành công mẫu tin !";
                OpenDialog("#thongbao");
                updatePanelThongBao.Update();
                BindGridclsSalesReport();
                BindGridSaleReportDetail_Edit(int.Parse(lbID.Text));

                Total_All();
            }
            catch (Exception ex)
            {
                lbThongBaoLoi.Text = ex.ToString();
                OpenDialog("#thongbao");
                updatePanelThongBao.Update();
            }
        }
        protected void Fillter(string DateBegin, string EndBegin)
        {
            dtSaleReportFillter = clsSalesReport.TMSalesReport_Fillter_Date(txtTuNgayNgayNhapKho.Text, txtDenNgayNhapKho.Text);
            if (dtSaleReportFillter != null && dtSaleReportFillter.Rows.Count > 0)
            {
                ListViewAll.DataSource = dtSaleReportFillter;
                ListViewAll.DataBind();
                ListViewAll.Visible = true;
                DataPagerListAll.Visible = true;

                ListViewExport.DataSource = dtSaleReportFillter;
                ListViewExport.DataBind();
                ListViewExport.Visible = true;

                StrAlert += "<div class='alert alert-success'>";
                StrAlert += "<a href='#' class='close' data-dismiss='alert'>&times;</a>";
                StrAlert += "Tìm thấy " + dtSaleReportFillter.Rows.Count.ToString() + " mẫu tin !";
                StrAlert += "</div>";
            }
            else
            {
                ListViewAll.Visible = false;
                DataPagerListAll.Visible = false;

                ListViewExport.Visible = false;

                StrAlert += "<div class='alert alert-error'>";
                StrAlert += "<a href='#' class='close' data-dismiss='alert'>&times;</a>";
                StrAlert += "Không tìm thấy mẫu tin nào !";
                StrAlert += "</div>";
            }
        }

        //AutoComplate
        protected void Autocomplate()
        {
            dtAutoComplete = clsSalesReport.BindGirdTMSalesReport();
            if (dtAutoComplete != null && dtAutoComplete.Rows.Count > 0)
            {
                foreach (DataRow dr in dtAutoComplete.Rows)
                {
                    if (string.IsNullOrEmpty(SuggestionList))
                    {
                        SuggestionList += "\"" + dr["FullNameMem"].ToString().Replace("\n", " ") + "\",";
                        SuggestionList += "\"" + dr["VehicleGroup_Name_Old"].ToString().Replace("\n", " ") + "\",";
                        SuggestionList += "\"" + dr["Vehicle_Number"].ToString().Replace("\n", " ") + "\",";
                        SuggestionList += "\"" + dr["Customers_Name"].ToString().Replace("\n", " ") + "\",";
                        SuggestionList += "\"" + dr["DommandTransferOrder_Alias"].ToString().Replace("\n", " ") + "\",";
                    }
                    else
                    {
                        SuggestionList += "\"" + dr["FullNameMem"].ToString().Replace("\n", " ") + "\",";
                        SuggestionList += "\"" + dr["VehicleGroup_Name_Old"].ToString().Replace("\n", " ") + "\",";
                        SuggestionList += "\"" + dr["Vehicle_Number"].ToString().Replace("\n", " ") + "\",";
                        SuggestionList += "\"" + dr["Customers_Name"].ToString().Replace("\n", " ") + "\",";
                        SuggestionList += "\"" + dr["DommandTransferOrder_Alias"].ToString().Replace("\n", " ") + "\",";
                    }
                }
            }
        }
        protected void Search(string keywords)
        {
            dtSearch = clsSalesReport.Search(keywords);
            if (dtSearch != null && dtSearch.Rows.Count > 0)
            {
                ListViewAll.DataSource = dtSearch;
                ListViewAll.DataBind();
                ListViewAll.Visible = true;
                DataPagerListAll.Visible = true;

                ListViewExport.DataSource = dtSearch;
                ListViewExport.DataBind();
                ListViewExport.Visible = true;

                StrAlert += "<div class='alert alert-success'>";
                StrAlert += "<a href='#' class='close' data-dismiss='alert'>&times;</a>";
                StrAlert += "Tìm thấy " + dtSearch.Rows.Count.ToString() + " mẫu tin !";
                StrAlert += "</div>";
            }
            else
            {
                ListViewAll.Visible = false;
                DataPagerListAll.Visible = false;

                ListViewExport.Visible = false;
                StrAlert += "<div class='alert alert-error'>";
                StrAlert += "<a href='#' class='close' data-dismiss='alert'>&times;</a>";
                StrAlert += "Không tìm thấy mẫu tin nào !";
                StrAlert += "</div>";
            }
        }
        //Total
        protected void Total_All()
        {
            dtTotal_All = clsSalesReport.Total();
            if (txtTuNgayNgayNhapKho.Text == "" && txtDenNgayNhapKho.Text == "")
            {
                if (dtTotal_All != null && dtTotal_All.Rows.Count > 0)
                {
                    StrTOTAL_Revenue = String.Format("{0:0,00}", dtTotal_All.Rows[0]["TOTAL_Revenue"]).Replace(".", ",") + " VNĐ";
                    StrTOTAL_COSTS = String.Format("{0:0,00}", dtTotal_All.Rows[0]["TOTAL_COSTS"]).Replace(".", ",") + " VNĐ";
                    StrTOTAL_Profit_Loss = String.Format("{0:0,00}", dtTotal_All.Rows[0]["TOTAL_Profit_Loss"]).Replace(".", ",") + " VNĐ";
                }
            }
            else
            {
                Total(txtTuNgayNgayNhapKho.Text, txtDenNgayNhapKho.Text);
            }
        }
        protected void Total(string DateBegin, string EndBegin)
        {
            dtTotal = clsSalesReport.TotalAll(txtTuNgayNgayNhapKho.Text, txtDenNgayNhapKho.Text);
            if (dtTotal != null && dtTotal.Rows.Count > 0)
            {
                StrTOTAL_Revenue = String.Format("{0:0,00}", dtTotal.Rows[0]["TOTAL_Revenue"]).Replace(".", ",") + " VNĐ";
                StrTOTAL_COSTS = String.Format("{0:0,00}", dtTotal.Rows[0]["TOTAL_COSTS"]).Replace(".", ",") + " VNĐ";
                StrTOTAL_Profit_Loss = String.Format("{0:0,00}", dtTotal.Rows[0]["TOTAL_Profit_Loss"]).Replace(".", ",") + " VNĐ";
            }
        }
        #endregion

        #region[OpenDialog]
        private void OpenDialog(string dialogId)
        {
            System.Text.StringBuilder sb = new System.Text.StringBuilder();
            sb.Append("$('" + dialogId + "').dialog('open');");
            ScriptManager.RegisterStartupScript(this.Page, this.Page.GetType(),
               "scriptKey", sb.ToString(), true);
        }
        private void CloseDialog(string dialogId)
        {
            System.Text.StringBuilder sb = new System.Text.StringBuilder();
            sb.Append("$('" + dialogId + "').dialog('close');");
            ScriptManager.RegisterStartupScript(this.Page, this.Page.GetType(),
               "scriptKey", sb.ToString(), true);
        }
        #endregion

        #region[Permisstion]
        protected void CheckPermisstion()
        {
            HttpCookie cookie = Request.Cookies["UserLogin"];
            if (cookie != null)
            {
                bool Is_Page_All = SecurityServices.HasPermisstionPage(StringContent.QuanLyDoanhThuChiPhi, int.Parse(cookie["RolesID"]), true);
                if (Is_Page_All == true)
                {
                    bool Edit = SecurityServices.HasPermisstion(StringContent.QuanLyDoanhThuChiPhi, int.Parse(cookie["RolesID"]), TypeaAudit.Edit);
                    if (Edit == false)
                    {
                        cmdEdit.Visible = false;
                    }
                    else
                    {
                        cmdEdit.Visible = true;
                    }
                    bool Del = SecurityServices.HasPermisstion(StringContent.QuanLyDoanhThuChiPhi, int.Parse(cookie["RolesID"]), TypeaAudit.Delete);
                    if (Del == false)
                    {
                        cmdDel.Visible = false;
                    }
                    else
                    {
                        cmdDel.Visible = true;
                    }
                }
                else
                {
                    Response.Redirect("error-page.aspx");
                }
            }
        }
        #endregion
    }
}